package com.ptswitch.material.components;

import java.math.BigDecimal;
import java.util.List;

import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.annotations.BeginRender;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.corelib.base.AbstractField;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;

import com.ptswitch.material.model.contract.ContractItem;
import com.ptswitch.material.model.material.Material;
import com.ptswitch.material.util.MaterialUtil;

public class CreateContractItems extends AbstractField {
    @Parameter(required = true, allowNull = false)
    private List<ContractItem> value;

    @Parameter(required = true, allowNull = false)
    private List<Material> source;
    @Inject
    private Request request;

    @BeginRender
    void begin(MarkupWriter writer) {
        writer.element("table", "class", "list");
        writer.element("thead");
        writer.element("tr");
        writer.element("th");
        writer.write("物料编码");
        writer.end();
        writer.element("th");
        writer.write("物料名称");
        writer.end();
        writer.element("th");
        writer.write("规格型号");
        writer.end();
        writer.element("th");
        writer.write("器件描述");
        writer.end();
        writer.element("th");
        writer.write("PCB封装");
        writer.end();
        writer.element("th");
        writer.write("采购状态");
        writer.end();
        writer.end();//end tr
        writer.end();//end thead
        writer.element("tbody");
        int i = 0;
        for (Material material : source) {
            writer.element("tr");
            writer.element("td");
            writer.element("a", "class", "forward", "target", "_blank", "href", request.getContextPath() + "/material/view/" + material.getId());
            writer.write(material.getCode());
            writer.end();//end a
            writer.end();
            writer.element("td");
            writer.write(material.getName());
            writer.end();
            writer.element("td");
            writer.write(material.getSpecification());
            writer.end();
            writer.element("td");
            writer.write(material.getDescription());
            writer.end();
            writer.element("td");
            writer.write(material.getPcbDecal());
            writer.end();
            writer.element("td");
            writer.write(material.getState());
            writer.end();
            writer.end();//end tr
            writer.element("tr");
            writer.element("td");
            writer.writeRaw("数量：<br />");
            writer.element("input", "type", "text", "name", "item" + i + ".quantities", "value", 0);
            writer.end();
            writer.end();
            writer.element("td");
            writer.writeRaw("含税单价：<br />");
            writer.element("input", "type", "text", "name", "item" + i + ".unitPriceIncludingTax", "value", 0);
            writer.end();
            writer.end();
            writer.element("td");
            writer.writeRaw("税率(%)：<br />");
            writer.element("input", "type", "text", "name", "item" + i + ".taxRate", "value", 0);
            writer.end();
            writer.end();
            writer.element("td", "colspan", "3");
            writer.writeRaw("备注：<br />");
            writer.element("input", "type", "text", "name", "item" + i + ".remark", "value", 0);
            writer.end();
            writer.end();
            writer.end();//end tr
            i++;
        }
        writer.end();//end tbody
        writer.end();//end table
    }

    private int getParameterIntValue(String name) {
        return Integer.valueOf(request.getParameter(name));
    }

//	private double getParameterDoubleValue(String name) {
//		return Double.valueOf(request.getParameter(name));
//	}

    private BigDecimal getParameterBigDecimalValue(String name) {
        return new BigDecimal(request.getParameter(name));
    }

    @Override
    protected void processSubmission(String elementName) {
        int itemSize = value.size();
        value = MaterialUtil.toContractItems(source);
        ContractItem contractItem;
        for (int i = itemSize - 1; i >= 0; i--) {
            contractItem = value.get(i);
            contractItem.setQuantities(getParameterIntValue("item" + i + ".quantities"));
            contractItem.setUnitPriceIncludingTax(getParameterBigDecimalValue("item" + i + ".unitPriceIncludingTax"));
            contractItem.setTaxRate(getParameterBigDecimalValue("item" + i + ".taxRate"));
            contractItem.setRemark(request.getParameter("item" + i + ".remark"));
            if (contractItem.getQuantities() <= 0) {
                value.remove(i);
            }
        }
    }
}
